<?php

namespace App\Http\Controllers\Client\Auth;

use Illuminate\Http\Request;
use App\Services\Auth\PersonalTokenService;
use App\Http\Controllers\RootController as Controller;

class ClientLoginController extends Controller
{
    public function handle(Request $request)
    {
        $param     = $request->only(['client_id', 'client_secret']);
        $rules    = [
            'client_id'     => 'required',
            'client_secret' => 'required',
        ];
        $messages = [
            'client_id.required'     => '客户端ID不能为空',
            'client_secret.required' => '客户端秘钥不能为空',
        ];

        if (true !== ($error = $this->validator($param, $rules, $messages)))
        {
            return $this->failure($error);
        }

        list($statusCode, $content) = PersonalTokenService::make($param['client_id'], $param['client_secret']);

        return (200 === $statusCode) ? $this->success($content) : $this->failure($content);
    }
}
